package com.woniuxy.yoga.coach.service.mq;

import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.redis.util.rediskeyutil.CoachRedisKeyUtil;
import com.woniuxy.yoga.commons.sms.util.SMSUtil;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class SendCodeConsumer {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private SMSUtil smsUtil;

    //教练登录发送验证码的消息队列
    @RabbitListener(queuesToDeclare = {@Queue(name = "coachLogin.sendMassage")})
    @RabbitHandler
    public void comsumer(Map<String,Object> message) throws Exception {
        String coachPhone = (String) message.get("coachPhone");
        //获取发送的验证码
        String code = smsUtil.send(coachPhone);
        //给验证码设置过期时间 3分钟
        redisUtil.set(CoachRedisKeyUtil.coachLoginCodeKey(coachPhone),code,3*60);
        //判断一分钟时间之内只能发送一次
        redisUtil.set(CoachRedisKeyUtil.coachLoginCodeOneMinute(coachPhone),code,60);
        //判断一个小时之内是否已经发送了五次验证码
        if (redisUtil.hasKey(CoachRedisKeyUtil.coachSendNumbers(coachPhone))){
            //存在
            redisUtil.incr(CoachRedisKeyUtil.coachSendNumbers(coachPhone));
        }else {
            //不存在
            redisUtil.set(CoachRedisKeyUtil.coachSendNumbers(coachPhone),"1",60*60);
        }
    }
}
